var session = sessionStorage.getItem("session");
if (!session) {
	window.parent.location = "../../login.html";
}
$(document).ready(function() {
	getTaskType();
	jQuery("#jqGrid").jqGrid({
		url: IP + "/QueryByProbe?callback=?&session=a",
		datatype: "jsonp",
		colModel: [{
			label: '序号',
			sorttype: 'integer',
			name: 'id',
			key: true,
			width: 50
		}, {
			label: '探针编号',
			name: 'deviceid',
			width: 120
		}, {
			label: '任务类型',
			name: 'type',
			width: 120
		}, {
			label: '设备组名',
			name: 'devgroupname',
			width: 120
		}, {
			label: '任务模板名称',
			name: 'modelname',
			width: 120
		}, {
			label: '安装地址',
			name: 'install',
			width: 120
		}, {
			label: '成功率',
			name: 'success_rate',
			width: 120
		}, {
			label: '平均延时',
			name: 'avgLatency',
			width: 120
		}, {
			label: '失败次数',
			name: 'losstimes',
			width: 120
		}, {
			label: '总次数',
			name: 'alltimes',
			width: 120
		}, {
			label: '得分',
			name: 'quality_score',
			width: 120
		}],
		loadonce: false,
		height: 350,
		rowNum: 13,
		page: 1,
		viewrecords: true,
		pagerpos: 'center',
		pgbuttons: true,
		pginput: true,
		loadtext: '正在加载数据',
		pager: "#jqGridPager"
	});
});

function check() {
	if ($("#probeDetail").val() == "") {
		$.messager.show({
			title: '提示！',
			msg: '请选择探针编号！',
			showType: 'fade',
			style: {
				right: '',
				bottom: ''
			}
		});
		return false;
	} else if ($("#taskType").combobox("getValue") == "") {
		$.messager.show({
			title: '提示！',
			msg: '请选择任务类型！',
			showType: 'fade',
			style: {
				right: '',
				bottom: ''
			}
		});
		return false;
	} else if ($("#sDate").datetimebox("getValue") == "") {
		$.messager.show({
			title: '提示！',
			msg: '请选择开始时间！',
			showType: 'fade',
			style: {
				right: '',
				bottom: ''
			}
		});
		return false;
	} else if ($("#eDate").datetimebox("getValue") == "") {
		$.messager.show({
			title: '提示！',
			msg: '请选择结束时间！',
			showType: 'fade',
			style: {
				right: '',
				bottom: ''
			}
		});
		return false;
	} else if ($("#sj").val() == "") {
		$.messager.show({
			title: '提示！',
			msg: '请选择时间粒度！',
			showType: 'fade',
			style: {
				right: '',
				bottom: ''
			}
		});
		return false;
	}
}

function find() {
	if (check() == false) {
		return;
	}
	alert(1); 
	$("#jqGrid").jqGrid('GridUnload')
//	$("#jqGrid").jqGrid("GridDestroy");
	alert(2);
	$("#jqGrid").jqGrid({
		url: IP + "/QueryByProbe?callback=?&session=" + session + "&device=" + $("#probeDetail").val() + "&tasktype=" + $("#taskType").combobox("getValue") + "&sdate=" + $("#sDate").datetimebox("getValue") + "&edate=" + $("#eDate").datetimebox("getValue") + "&sj=" + $("#sj").val() + "&unit=" + $("#unit").combobox("getValue"),
		datatype: "jsonp",
		colModel: [{
			label: '序号',
			sorttype: 'integer',
			name: 'id',
			key: true,
			width: 50
		}, {
			label: '探针编号',
			name: 'deviceid',
			width: 120
		}, {
			label: '任务类型',
			name: 'type',
			width: 120
		}, {
			label: '设备组名',
			name: 'devgroupname',
			width: 120
		}, {
			label: '任务模板名称',
			name: 'modelname',
			width: 120
		}, {
			label: '安装地址',
			name: 'install',
			width: 120
		}, {
			label: '成功率',
			name: 'success_rate',
			width: 120
		}, {
			label: '平均延时',
			name: 'avgLatency',
			width: 120
		}, {
			label: '失败次数',
			name: 'losstimes',
			width: 120
		}, {
			label: '总次数',
			name: 'alltimes',
			width: 120
		}, {
			label: '得分',
			name: 'quality_score',
			width: 120
		}],
		loadonce: false,
		height: 350,
		rowNum: 13,
		page: 1,
		viewrecords: true,
		pagerpos: 'center',
		pgbuttons: true,
		pginput: true,
		loadtext: '正在加载数据',
		pager: "#jqGridPager"
	});
	showHistogram();
	showPie();
}

function showDialog() {
	$("#probeTable").dialog("open");
	jQuery("#pGrid").jqGrid({
		url: IP + "GetProbeInfoList?callback=?&session=" + session,
		datatype: "jsonp",
		height: 230,
		width: 260,
		colNames: ['设备名称', '设备唯一编码'],
		colModel: [{
			name: 'deviceName',
			index: 'deviceName',
			sopt: ['eq'],
			width: 30
		}, {
			name: 'deviceid',
			index: 'deviceid',
			sopt: ['eq'],
			width: 40
		}],
		autowidth: false,
		shrinkToFit: true,
		//		shrinkToFit:false,
		loadonce: true,
		mtype: "GET",
		multiselect: true,
		multiselectWidth: 20,
		page: 1,
		colMenu: true,
		viewrecords: true
			//		sortorder: "asc",
			//		sortname: 'id'
	});
	var timer;
	$("#SearchCellPingId").on("keyup", function() {
		var self = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function() {
			//timer = null;
			$("#pGrid").jqGrid('filterInput', self.value);
		}, 0);
	});

};

//选取弹针类型；
function searchProbe(current, at, state) {
	$("#probeDetail").empty();
	var arr = [];
	var Ids = $(current).jqGrid("getGridParam", "selarrrow");
	var len = Ids.length
	if (len == 0) {
		$.messager.show({
			title: '提醒',
			msg: '您什么也没选',
			timeout: 1300,
			showType: 'slide',
			style: {
				right: '',
				bottom: ''
			}
		});
		return false;
	} else {
		for (var i = 0; i < len; i++) {
			var getRow = $(current).getRowData(Ids[i]);
			arr.push(getRow[at]);
		}
		var str = arr.join(",");
		$("#probeDetail").textbox('setValue', str);
		$("#probeTable").dialog("close");
		$(current).trigger("reloadGrid");
		flag = state;
	}
};

function getTaskType() {
	$.ajax({
		url: IP + "GetSystemParam?&callback=?&flag=1001" + "&session=" + session,
		type: "Get",
		dataType: "jsonp",
		jsonp: "callback",
		jsonpCallback: "resultHandler",
		success: function(data) {
			//			var options = data.vValue.split(",");
			//			var frag = document.createDocumentFragment("fragElement");
			//			var op = document.createElement("OPTION");
			//			op.text = "All";
			//			frag.appendChild(op);
			//			for (var i = 0; i < options.length; i++) {
			//				var opObj = document.createElement("OPTION");
			//				opObj.text = options[i];
			//				opObj.value = options[i];
			//				frag.appendChild(opObj);
			//			}
			//			$("#taskType").append(frag);

			var options = data.vValue.split(",");
			var sel = [];
			for (var i = 0; i < options.length+1; i++) {
				var n = [];
				if(i==0){
					n.push("All");
					sel.push(n);
				}else{
					n.push(options[i-1]);
					sel.push(n);
				}
			}
			$("#taskType").combobox({
				data: sel,
				valueField: 0,
				textField: 0,
				editable: false,
				onSelect: function() {
//					if (currentgroupid > 0) {
//						var getRow = $("#taskGroupList").getRowData(currentgroupid);
//						console.log(getRow.type);
//						$("#testType").combobox("setValue", getRow.type);
//					}
//					var $table = $("<table id='testTypeList'><table><div id='testTypeListPager'></div>");
//					$("#myBox").empty().html($table);
//					var val = $("#testType").combobox("getValue");
//					getTestParamList(val, currentgroupid);
				}
			});
			var a = ['分钟','小时','天','周','月'];
			$("#unit").combobox({
				data: a,
				valueField: 0,
				textField: 0,
				editable: false});
				$("#unit").combobox("setValue", a[0]);
			$("#taskType").combobox("setValue", sel[0]);
		}
	});

};
